package com.lianup.noname.servlet;

import com.lianup.noname.util.StringUtil;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Servlet测试类
 */
public class RegisterServlet extends HttpServlet{


    private static final long serialVersionUID = 1123261262294483141L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        System.out.println(name);
        System.out.println(password);
        if(StringUtil.isBlank(name) || StringUtil.isBlank(password)){
            try {
                response.getWriter().println("invalid parameter!");
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        System.out.println("parse user register  method :" + request.getMethod());
        System.out.println("parse user register cookies:");
        Optional.ofNullable(request.getCookies())
                .ifPresent(cookies -> Stream.of(cookies).forEach(cookie->System.out.println(cookie.getName() + ":" + cookie.getValue())));

        System.out.println("parse http header:");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String haedName = headerNames.nextElement();
            System.out.println(headerNames + ":" + request.getHeader(haedName));
        }

        try {
            response.getWriter().println("register successfully!");
        }catch (Exception e){
            e.printStackTrace();
        }


    }

    @Override
    public void doPost(HttpServletRequest request,HttpServletResponse response){
        doGet(request,response);
    }
}
